/**
 *******************************************************************************
 * @FileName  : timer.c
 * @Author    : GaoQiu
 * @CreateDate: 2020-05-18
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 *******************************************************************************
 */
#include "timer.h"
#include "platform/platform.h"

#define TIM_ID          TIM0
#define GPIOx           GPIOC
#define GPIO_PIN        GPIO_Pin_2
#define IRQ_TIMX        IRQ_TIM0


void TIMER_SetUp(void)
{
	/* GPIO config. */
	GPIO_InitType_t GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Pin  = GPIO_PIN;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out;
	GPIO_InitStruct.GPIO_Pull = GPIO_Pull_Floating;
	GPIO_Init(GPIOx, &GPIO_InitStruct);

	/* Timer */
	TIM_Init(TIM_ID, TIM_Mode_Sys_Clk);
	TIM_SetCapture(TIM_ID, 100*1000*48);//period = 100ms
	TIM_SetCounter(TIM_ID, 0);
	TIM_Cmd(TIM_ID, ENABLE);

	/* Enabe timer peripheral interrupt. */
	IRQ_EnablePeriphIrq(IRQ_TIMX);
}

void TIMER_Loop(void)
{

}

volatile uint32_t A_timrIrq = 0;
void TIMER_IRQHandler(void)
{
	/* Timer */
	if(TIM_GetITFlag(TIM_ID))
	{
		TIM_ClearITFlag(TIM_ID);
		//TIM_SetCapture(TIM0, 100*1000*16);
		GPIO_ToggleBits(GPIOx, GPIO_PIN);
		A_timrIrq++;
	}
}
